package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: 付诚
 * @Date: 2024/5/21 14:3
 * @Description:
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public List<MarketTopic> list(Integer page, Integer limit) {

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andLogicalDeleted(true);
        marketTopicExample.setOrderByClause("add_time desc");
        PageHelper.startPage(page, limit);
        return topicMapper.selectByExample(marketTopicExample);
    }

    @Override
    public Map<String, Object> detail(Integer id) {

        Map<String, Object> data = new HashMap<>();
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        data.put("topic", marketTopic);

        Integer[] goods = marketTopic.getGoods();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        List<MarketGoods> marketGoods = null;
        if(goods.length != 0) {
            criteria.andIdIn(Arrays.asList(goods));
            marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        }
        ArrayList<HashMap<String, Object>> returnGoodsList = new ArrayList<>();
        if(marketGoods != null) {
            for (MarketGoods marketGood : marketGoods) {
                HashMap<String, Object> goodsMap = new HashMap<>();
                goodsMap.put("id", marketGood.getId());
                goodsMap.put("name", marketGood.getName());
                goodsMap.put("brief", marketGood.getBrief());
                goodsMap.put("picUrl", marketGood.getPicUrl());
                goodsMap.put("isNew", marketGood.getIsNew());
                goodsMap.put("isHot", marketGood.getIsHot());
                goodsMap.put("counterPrice", marketGood.getCounterPrice());
                goodsMap.put("retailPrice", marketGood.getRetailPrice());

                returnGoodsList.add(goodsMap);
            }
        }
        data.put("goods", returnGoodsList);
        return data;
    }

    @Override
    public List<MarketTopic> related(Integer id) {

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andLogicalDeleted(true);
        criteria.andIdNotEqualTo(id);
        PageHelper.startPage(1, 4);
        return topicMapper.selectByExample(marketTopicExample);
    }
}
